<?php
class AdMenuAbstract extends FrontAction {

	public static function getOrderMenu() {
		$menuModule = new AdminModule();
		$menu = $menuModule->getAllMenu('menuid,type,name,action_key,action,parentid',-1, null, null); //取出所有的列表
		$result = null;
		foreach($menu as $val) {
			$result[$val['parentid']][] = $val;
		}

		return $result;
	}

	/**
	 * 生成下拉列表--包含所有类别
	 * @param $nowValue
	 * @param $keyFiled
	 * @param $valueFiled
	 */
	public static function makeAllMenuSelect($nowValue = '', $keyFiled = '', $valueFiled = '', $isDisabled = ''){
		if(empty($keyFiled)){
			$keyFiled = 'menuid';
		}
		if(empty($valueFiled)){
			$valueFiled = 'name';
		}
		if(empty($isDisabled)){
			$isDisabled = 'isDisabled';//是否要disabled当前下拉选项
		}
		$data = self::getAllMenuData();

		$rs = self::makeSelectOption($data, $nowValue, $keyFiled, $valueFiled, $isDisabled);
		return $rs;
	}

	/**
	 * 生成下拉选择的选择内容，不包括 <select ****></select>
	 * @param array $data            内容，一维数组 或  二维数组，二维数组时第一维为个数，第二维为内容
	 * @param string $nowValue      当前值
	 * @param string $keyFiled        二维数组时用来做 key 的字段名称
	 * @param string $valueFiled    二维数组时用来做 value 的字段名称
	 *
	 *
	 */
	private static function  makeSelectOption($data, $nowValue = '', $keyFiled = '', $valueFiled = '', $isDisabled = '')
	{
		$result = "";
		foreach ( $data as $key => $value ) {
			if (is_array ( $value )) {
				if (! empty ( $keyFiled ) and ! empty ( $valueFiled )) {

					if (($value [$keyFiled] == $nowValue || $value [$valueFiled] === $nowValue) && $value [$keyFiled] != NULL) {

						if(isset($value [$isDisabled]) && $value [$isDisabled] == "true"){
							$result .= "<option disabled='disabled' value='" . htmlentities ( $value [$keyFiled] ) . "' selected>" . strip_tags ( $value [$valueFiled] ) . "</option>";
						}else{
							$result .= "<option value='" . htmlentities ( $value [$keyFiled] ) . "' selected>" . strip_tags ( $value [$valueFiled] ) . "</option>";
						}
					} else {
						//echo "222|";
						if(isset($value [$isDisabled]) && $value [$isDisabled] == "true"){
							$result .= "<option disabled='disabled' value='" . htmlentities ( $value [$keyFiled] ) . "' >" . strip_tags ( $value[$valueFiled] ) . "</option>";
						}else{
							$result .= "<option value='" . htmlentities ( $value [$keyFiled] ) . "'>" . strip_tags ( $value[$valueFiled] ) . "</option>";
						}
					}
				}
			} else {
				echo 111;
				if ($key == $nowValue || $value == $nowValue) {
					if(isset($value [$isDisabled]) && $value [$isDisabled] == "true"){
						$result .= "<option disabled='disabled' value='" . htmlentities ( $key ) . "' selected>" . strip_tags ( $value ) . "</option>";
					}else{
						$result .= "<option value='" . htmlentities ( $key ) . "'>" . strip_tags ( $value ) . "</option>";
					}
				} else {
					if(isset($value [$isDisabled]) && $value [$isDisabled] == "true"){
						$result .= "<option disabled='disabled' value='" . htmlentities ( $key ) . "' >" . strip_tags ( $value ) . "</option>";
					}else{
						$result .= "<option value='" . htmlentities ( $key ) . "'>" . strip_tags ( $value ) . "</option>";
					}
				}
			}
		}
		return $result;
	}

	/**
	 * 生成下拉数据--包含所有菜单
	 */
	private static function getAllMenuData(){
		$data = array();
		$parentData = array();

		//$parentMenu = $this->getAllMenu("0");//父菜单数据

		$menu = self::getOrderMenu();

		if(!empty($menu)) {
			//存在父级
			if(key_exists(0,$menu) && !empty($menu[0])) {
				$parentMenu = $menu[0];
			}
		}

		if(!empty($parentMenu)){
			foreach ($parentMenu as $parent) {
				$tmp = array();
				$menuid = $parent['menuid'];
				$tmp['menuid'] = $menuid;
				$tmp['name'] = $parent['name'];//父菜单Name
				//$tmp['isDisabled'] = 'true';
				//获取子菜单数据
				//$childrenMenu = $this->getAllMenu($menuid);

				$childrenMenu =null;
				if(key_exists($menuid,$menu) && !empty($menu[$menuid])) {
					$childrenMenu = $menu[$menuid];
				}

				if(!empty($childrenMenu)){
					$count = count($childrenMenu);
					$i = 0;
					$childrenData = array();
					foreach ($childrenMenu as $children){
						$i ++;
						if($count == 1){
							$link = ' &nbsp;&nbsp;&nbsp;<b>[</b> ';
						}else if($i == 1 && $count > 1){
							$link = ' &nbsp;&nbsp;&nbsp;┏ ';
						}else if($i == $count && $count>1){
							$link = ' &nbsp;&nbsp;&nbsp;┗ ';
						}else{
							$link = ' &nbsp;&nbsp;&nbsp;┣ ';
						}
						$childTmp = array();
						$childTmp['menuid'] = $children['menuid'];
						$childTmp['name'] = $link.$children['name'];//子类别Name
						$childTmp['isshow'] = isset($children['isshow']) ? $children['isshow'] : '';//子类别是否显示
						$childrenData[] = $childTmp;
					}
					$tmp['childmenu'] = $childrenData;
				}
				$parentData[] = $tmp;
			}
		}
		if(!empty($parentData)){
			foreach ($parentData as $k=>$p){
				$data[] = $p;
				if(!empty($p['childmenu']) && is_array($p['childmenu'])){
					foreach ($p['childmenu'] as $c) {
						$t = array();
						$t['menuid'] = $c['menuid'];
						$t['name'] = $c['name'];//父菜单Name
						$t['isshow'] = isset($c['ishow']) ? $c['ishow'] : '';
						$data[] = $t;
					}
					unset($data[$k]['childclass']);
				}
			}
		}
		array_unshift($data, array('menuid'=>0,'name'=>'根菜单'));
		return $data;
	}

}
